<?php

namespace App\Http\Controllers\Personal;

use App\Model\Leave;
use App\Model\Sch_desc;
use App\Model\Staff_task;
use App\Model\Users;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
//个人信息
class Personal_infoController extends CommonController
{
    //

	protected $Leave;
	protected $Users;
	protected $Staff_task;
	protected $Sch_desc;
	public function __construct()
	{
		parent::__construct();
		$this->Leave=new Leave();
		$this->Users=new Users();
		$this->Staff_task=new Staff_task();
		$this->Sch_desc=new Sch_desc();
	}


//	历史任务列表
	public function history_index(Request $request){
		$type=['申请中','接单失败','等待排班','进行中','已结束','任务终止'];
		$firstday = date('Y-m-01', strtotime($request->time)); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		$where['start_time']=$firstday;
		$where['end_time']=$lastday;
		$where['status']=isset($request->status)?$request->status:1;
		$where['uid']=Auth::user()->id;
		$data=$this->Staff_task->history_task($where);
		if(count($data)){
			foreach($data as $kk=>$vv){
				$newArr[$kk]['station_name']=$vv['task']['store_station']['name']; //任务名称
				$newArr[$kk]['status']=''; //任务期限
				$status=$vv['store_station']['status'];
				if($status=0){
					$newArr[$kk]['status']='短期有效';
				}elseif ($status=1){
					$newArr[$kk]['status']='长期有效';
				}elseif($status=-1){
					$newArr[$kk]['status']='失效';
				}
				$newArr[$kk]['start_time']=$vv['task']['start_day'];//任务周期开始日期
				$newArr[$kk]['end_time']=$vv['task']['end_day'];//任务周期结束日期
				$newArr[$kk]['task_status']=$type[$vv['status']-1];//任务状态
				$newArr[$kk]['id']=$vv['id'];//员工任务id
			}
		}else{
			$newArr=[];
		}
		return $this->success($newArr);
	}

//	历史任务详情
	public function history_desc(Request $request){
		$id=$request->id;
//		$data=$this->Staff_task->where(['id'=>$id])->first();
		$data=$this->Staff_task->history_task_desc($id);
//		return $data;
		$newArr['station_name']=$data['task']['store_station']['name'];
		$status=$data['task']['store_station']['status'];
		if($status==-1) {
			$newArr['status'] = '失效';
		}elseif ($status==0){
			$newArr['status'] = '短期有效';
		}elseif ($status==1){
			$newArr['status'] = '长期有效';
		}
		$newArr['task_start']=$data['task']['start_day'];
		$newArr['task_end']=$data['task']['end_day'];
		$newArr['leave_hour']=$this->Leave->where(['staff_task_id'=>$id])->sum('time');
		$newArr['sign_days']=count($this->Sch_desc->sign_staff_task($id));
		$newArr['experience']['department']=110;
		$newArr['experience']['sum']=120;
		return $this->success($newArr);
	}

//	我的钱包首页
	public function wallet_index(){
		return 1;
	}
}
